package com.fita.admin.dao;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.fita.entity.agent.Agent;
import com.fita.entity.data.Country;
import com.fita.entity.order.Order;
import com.fita.entity.order.OrderStatus;
import com.fita.entity.order.PaymentRequest;

import java.util.List;

/**
 * 
 * @author 宋剑宏
 *
 */
@Dao("hibernate")
public interface OrderDao {
	
	/**
	 * 计算资源
	 * @param status
	 * @return
	 */
	
	@Query("SELECT COUNT(*) FROM Order WHERE status = :status AND disabled = false")
	public int count(@Variable("status") OrderStatus status);

	/**
	 * @param agentIds
	 * @param status
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM Order WHERE agent.id in (:agentIds) AND status = :status AND disabled = false")
    public int count(@Variable("agentIds")List<Long> agentIds, @Variable("status") OrderStatus status);
	
    /**
     * @param agent
     * @param status
     * @return
     */
    @Query("SELECT COUNT(*) FROM Order WHERE agent = :agent AND status = :status")
    public int count(@Variable("agent") Agent agent, @Variable("status") OrderStatus status);
	
    /**
     * 根据状态和代理人查询订单列表
     * @param status
     * @return
     */
    @Limit
    @Query("FROM Order WHERE agent = :agent AND status = :status AND disabled = false")
    public List<Order> list(@Variable("agent") Agent agent, @Variable("status") OrderStatus status, @Variable("beginIndex") int beginIndex,@Variable("maxResult") int maxResult);
    
	/**
	 * 根据状态查询订单列表
	 * @param status
	 * @return
	 */
	@Limit
	@Query("FROM Order WHERE status = :status AND disabled = false ORDER BY id DESC")
	public List<Order> list(@Variable("status") OrderStatus status, @Variable("beginIndex") int beginIndex,@Variable("maxResult") int maxResult);
	
	/**
	 * 
	 * @param code
	 * @return
	 */
	@Query("FROM Country WHERE code =:code")
	public List<Country> load(@Variable("code") String code);
	
	/**
	 * 
	 * @param orderCode
	 * @return
	 */
	@Query("FROM PaymentRequest WHERE orderCode =:orderCode")
	public PaymentRequest loadByOrderCode(@Variable("orderCode") String orderCode);

	/**
	 * @param agentIds
	 * @param status
	 * @param beginIndex
	 * @param maxResult
	 * @return
	 */
	@Limit
	@Query("FROM Order WHERE agent.id in (:agentIds) AND status = :status AND disabled =false ORDER BY id DESC")
    public List<Order> listOrder(@Variable("agentIds")List<Long> agentIds, @Variable("status") OrderStatus status, @Variable("beginIndex") int beginIndex,@Variable("maxResult") int maxResult);

	@Query("SELECT COUNT(*) FROM Order WHERE agent in (:agents) AND status = :status AND disabled = false")
    public int countBySale(@Variable("agents") List<Agent> agents, @Variable("status") OrderStatus status);

	/**
	 * @param agents
	 * @param status
	 * @param beginIndex
	 * @param maxResult
	 * @return
	 */
	@Limit
    @Query("FROM Order WHERE agent in (:agents) AND status = :status AND disabled = false ORDER BY id DESC")
    public List<Order> list(@Variable("agents") List<Agent> agents, @Variable("status") OrderStatus status, @Variable("beginIndex") int beginIndex,@Variable("maxResult") int maxResult);

	@Query("SELECT COALESCE(SUM(actualPayPrice), 0) FROM Order WHERE status IN ('PAID', 'CONSUMED') AND agent = :agent")
    int sumActualPayPrice(@Variable("agent") Agent agent);
}
